Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
How can I check for "is not defined" eval(var) in JavaScript?

There are lot of info to check for "undefined" normal vars in JavaScript, but I need to check for an eval(var) like:

var Hello_0 = "Hello 0 !";

console.log(" Hello_0 is: " + eval("Hello_"+0));

var dinamyc_var = "Hello_"+1;

//None of the next conditionals is triggered///
if (eval(dinamyc_var) === undefined) {
     console.log(" dinamyc_var is undefined ");
} 
if (eval(dinamyc_var) === null) {
     console.log(" dinamyc_var is null ");
}
if ( typeof eval(created_node) === undefined ) {
     console.log(" dinamyc_var is typeof undefined ");
}
if ( typeof eval(created_node) === null ) {
     console.log(" dinamyc_var is typeof null ");
}

console.log(" dinamyc_var is: " + eval(dinamyc_var));

https://jsfiddle.net/mczdrv5t/

The console show the value of eval("Hello_"+0) because was declared, but the console show the next error when try to call eval("Hello_"+1) that not was declared. I need to handle possible undeclared vars.

Uncaught ReferenceError: Hello_1 is not defined at eval (eval at ....

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Don't use eval. If you need to store values of varying names, use an object instead:

const values = {}

values["Hello_0"] = "Hello 0 !"

if (values["Hello_" + 1] === undefined) {
  // ...
}

As a sidenote, if (eval('typeof ' + dynamic_var) === 'undefined') would work but again, don't use eval.

Don't do window[dynamic_var] = ... to store global variables either, the names might clash with existing globals.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda